package com.acme.service;

import java.util.List;

import com.acme.domain.account.Account;
import com.acme.domain.account.CheckingAccount;
import com.acme.domain.account.SavingAccount;
import com.acme.domain.bank.Bank;
import com.acme.domain.client.Client;
import com.acme.domain.client.Gender;
import com.acme.exceptions.ClientExistsException;
import com.acme.exceptions.NoEnoughFundsException;
import com.acme.exceptions.OverDraftLimitExceededException;

public class BankApplication {
	
	public static void main(String[] args) {
		Bank myBank = new Bank(10);

		for (int i = 0; i < 10; i++) {
			Client newClient;
			if (i % 2 == 0) {
				newClient = new Client(Integer.toString(i), Gender.MALE);
				newClient.addAccount(new SavingAccount(i, 100 + i * 10));
			} else {
				newClient = new Client(Integer.toString(i), Gender.FEMALE);
				newClient.addAccount(new CheckingAccount(i, 100 + i * 10, 100));
			}

			try {
				myBank.addClient(newClient);
			} catch (ClientExistsException cee) {
				System.out.println(cee);
			}
			System.out.println(newClient);
		}
		testEquals(myBank);
	}
	
	public static void testEquals(final Bank bank) {
		Client[] clients = bank.getClients();
		Client cl = new Client(Integer.toString(0), Gender.MALE);
		if (cl != clients[0]) {
			System.out.println("Instances of clients are not equal");
		}
		if (cl.equals(clients[0])) {
			System.out.println("Clients are equal");
		}
		
		List<Account> al = clients[0].getAccountsList();
		Account acc = new SavingAccount(0, 100);
		if (acc != al.get(0)) {
			System.out.println("Instances of accounts are not equal");
		}
		if (acc.equals(al.get(0))) {
			System.out.println("Accounts are equal");
		}
	}
	
	public static void testExceptions(final Bank bank) {
		try {
			BankService.depositToAll(bank, -100d);
		} catch (IllegalArgumentException iae) {
			System.out.println(iae);
		}

		try {
			BankService.withdrawFromSmb(bank, 0, 1000d);
		} catch (IllegalArgumentException iae) {
			System.out.println(iae);
		} catch (NoEnoughFundsException nefe) {
			System.out.println(nefe.getError());
		}

		try {
			BankService.withdrawFromSmb(bank, 0, -10d);
		} catch (IllegalArgumentException iae) {
			System.out.println(iae);
		} catch (NoEnoughFundsException nefe) {
			System.out.println(nefe.getError());
		}

		try {
			BankService.withdrawFromSmb(bank, 1, 100000d);
		} catch (IllegalArgumentException iae) {
			System.out.println(iae);
		} catch (OverDraftLimitExceededException olee) {
			System.out.println(olee);
		} catch (NoEnoughFundsException nefe) {
			System.out.println(nefe.getError());
		}

		try {
			BankService.withdrawFromSmb(bank, 1, -10d);
		} catch (IllegalArgumentException iae) {
			System.out.println(iae);
		} catch (OverDraftLimitExceededException olee) {
			System.out.println(olee);
		} catch (NoEnoughFundsException nefe) {
			System.out.println(nefe.getError());
		}
	}
}
